package cn.lrwjz.androidguide.ui.exmple;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import cn.lrwjz.androidguide.R;
import cn.lrwjz.androidguide.adapter.ViewPagerFragmentAdapter;
import cn.lrwjz.androidguide.ui.exmple.fragment.DashboardFragment;
import cn.lrwjz.androidguide.ui.exmple.fragment.HomeFragment;
import cn.lrwjz.androidguide.ui.exmple.fragment.NotificationsFragment;

public class ViewPagerWithNavActivity extends AppCompatActivity {
    private ViewPager2 viewPager;
    private BottomNavigationView bottomNavigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager_with_nav);

        // 设置工具栏
        MaterialToolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("ViewPager2 结合 Fragment 示例");
        }

        // 初始化ViewPager2
        viewPager = findViewById(R.id.view_pager);
        Fragment[] fragments = new Fragment[]{
                new HomeFragment(),
                new DashboardFragment(),
                new NotificationsFragment()
        };
        ViewPagerFragmentAdapter adapter = new ViewPagerFragmentAdapter(getSupportFragmentManager(), getLifecycle(), fragments);
        viewPager.setAdapter(adapter);

        // 禁用ViewPager2的滑动
//        viewPager.setUserInputEnabled(false);

        // 初始化底部导航
        bottomNavigationView = findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnItemSelectedListener(item -> {
            int itemId = item.getItemId();
            if (itemId == R.id.navigation_home) {
                viewPager.setCurrentItem(0);
                return true;
            } else if (itemId == R.id.navigation_dashboard) {
                viewPager.setCurrentItem(1);
                return true;
            } else if (itemId == R.id.navigation_notifications) {
                viewPager.setCurrentItem(2);
                return true;
            }
            return false;
        });

        // ViewPager2页面切换监听
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position) {
                    case 0:
                        bottomNavigationView.setSelectedItemId(R.id.navigation_home);
                        break;
                    case 1:
                        bottomNavigationView.setSelectedItemId(R.id.navigation_dashboard);
                        break;
                    case 2:
                        bottomNavigationView.setSelectedItemId(R.id.navigation_notifications);
                        break;
                }
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 